import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyHttpServer {
    //端口
    private int port=8080;
    //ip为服务器的ip
    //接收请求的方法
    public void receiving(){
        try {
            //创建Socket服务
            ServerSocket serverSocket=new ServerSocket(port);
            //循环接收请求
            while (true){
                //循环接收请求,获取 Socket 对象,获取到了连接对象
                Socket socket=serverSocket.accept();
                //获取连接对象的输入流
                InputStream inputStream=socket.getInputStream();
                //创建Request对象
                MyHttpRequest request=new MyHttpRequest(inputStream);
                //解析请求
                request.parse();
                //开始响应，创建Response
                //获取连接对象的输出流
                OutputStream outputStream=socket.getOutputStream();
                MyHttpResponse response=new MyHttpResponse(outputStream);
                //根据获取的uri进行响应
                response.sendRedirect(request.getUri());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
